//
//  IMPNavigationController.swift
//  MySina
//
//  Created by imppppp on 16/5/22.
//  Copyright © 2016年 IMP. All rights reserved.
//

import UIKit

class IMPNavigationController: UINavigationController, UIGestureRecognizerDelegate {
    
    
    override func viewDidLoad() {
        
        // 设置代理, 重新实现滑动功能
        self.interactivePopGestureRecognizer?.delegate = self
    }
    // 重写push 方法, 拦截push操作,设置返回按钮
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        var backTitle = "返回"
        if self.viewControllers.count > 0 {
            if self.viewControllers.count == 1 {
                
                backTitle = self.viewControllers.first?.title ?? "返回"
                
            }
            viewController.hidesBottomBarWhenPushed = true
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(backBtnClick))
        }
        
        
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func backBtnClick(){
        
        self.popViewControllerAnimated(true)
    }
    
    // 开启手势判断
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        return self.viewControllers.count > 1
    }
    
    
    
}
